package com.windchime.blog.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.windchime.blog.entity.ResourceType;
import com.windchime.blog.entity.vo.ResourceTypeSearch;
import com.windchime.blog.entity.vo.TypeOrTagVo;
import com.windchime.blog.result.R;
import com.windchime.blog.service.ResourceTypeService;
import com.windchime.blog.utils.StringSplitUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 资源分类 前端控制器
 * </p>
 *
 * @author WindChime
 * @since 2021-08-07
 */
@RestController
@RequestMapping("/admin/resource-type")
public class AdminResourceTypeController {

    @Autowired
    private ResourceTypeService resourceTypeService;

    /**
     * 分页查询--带条件
     * @param page 当前页
     * @param limit 当前页条目数
     * @param resourceTypeSearch 条件对象
     * @return resourceTypes
     */
    @GetMapping("/selectResourceTypeList")
    public R selectResourceTypeList(Integer page, Integer limit, ResourceTypeSearch resourceTypeSearch) {
        PageInfo<ResourceType> resourceTypes = resourceTypeService.selectResourceTypeList(page, limit, resourceTypeSearch);
        return R.ok().code(0).msg("").count(resourceTypes.getTotal()).data("resourceTypes", resourceTypes.getList());
    }

    /**
     * 添加资源分类
     * @param resourceType 资源分类
     * @return true
     */
    @PostMapping("/saveResourceType")
    public R saveResourceType(ResourceType resourceType) {
        boolean b = resourceTypeService.save(resourceType);
        if (b) {
            return R.ok().msg("添加成功");
        } else {
            return R.error().msg("添加失败");
        }
    }

    /**
     * 给添加页面赋值
     * @return names
     */
    @PostMapping("/selectParentNames")
    public R selectParentNamesForAdd() {
        List<ResourceType> resourceTypes = resourceTypeService.list(new QueryWrapper<ResourceType>().eq("flag", false).eq("parent_id", 0));
        List<TypeOrTagVo> res = resourceTypes.stream().map(resourceType -> new TypeOrTagVo(resourceType.getName(), resourceType.getId())).collect(Collectors.toList());
        return R.ok().data("resourceTypes", res);
    }

    /**
     * 根据一级分类ID查询二级分类
     * @param id 一级分类ID 等同于parent_id
     * @return twoTypes
     */
    @GetMapping("/selectTwoTypes/{id}")
    public R selectTwoTypes(@PathVariable Long id) {

        List<ResourceType> resourceTypes = resourceTypeService.list(new QueryWrapper<ResourceType>().eq("parent_id", id).eq("flag", true));
        List<TypeOrTagVo> res = new ArrayList<>();
        if (resourceTypes.size() == 0) {
            res.add(new TypeOrTagVo("无分类", 0));
        } else {
            res = resourceTypes.stream().map(resourceType -> new TypeOrTagVo(resourceType.getName(), resourceType.getId())).collect(Collectors.toList());
        }
        return R.ok().data("twoTypes", res);
    }

    /**
     * 给修改页面赋值
     * @return names
     */
    @GetMapping("/selectParentNames/{id}")
    public R selectParentNamesForEdit(@PathVariable Long id) {
        ResourceType resourceType = resourceTypeService.getById(id);
        List<TypeOrTagVo> res = new ArrayList<>();
        if (!resourceType.getFlag()) {
            res.add(new TypeOrTagVo("无", 0));
        } else {
            List<ResourceType> resourceTypes = resourceTypeService.list(new QueryWrapper<ResourceType>().eq("flag", false).eq("parent_id", 0));
            res = resourceTypes.stream().map(re -> new TypeOrTagVo(re.getName(), re.getId())).collect(Collectors.toList());
        }
        return R.ok().data("resourceTypes", res);
    }

    /**
     * 给修改页面赋值--回显已选中的目录类型
     * @return names
     */
    @GetMapping("/selectFlagChecked/{id}")
    public R selectFlagChecked(@PathVariable Long id) {
        ResourceType resourceType = resourceTypeService.getById(id);
        List<TypeOrTagVo> res = new ArrayList<>();
        if (resourceType.getFlag()) {
            res.add(new TypeOrTagVo("子目录", 1));
        } else {
            res.add(new TypeOrTagVo("根目录", 0));
        }
        return R.ok().data("resourceFlagSelect", res);
    }

    /**
     * 给修改页面赋值--回显已选中的所属分类
     * @return names
     */
    @GetMapping("/selectParentIdChecked/{id}")
    public R selectParentIdChecked(@PathVariable Long id) {
        ResourceType resourceType = resourceTypeService.getById(id);
        List<TypeOrTagVo> res = new ArrayList<>();
        ResourceType r = resourceTypeService.getById(resourceType.getParentId());
        if (r == null) {
            res.add(new TypeOrTagVo("无", 0));
        } else {
            res.add(new TypeOrTagVo(r.getName(), r.getId()));
        }
        return R.ok().data("resourceParentIdSelect", res);
    }

    /**
     * 修改资源分类
     * @param resourceType 资源分类
     * @return true
     */
    @PostMapping("/editResourceType")
    public R editResourceType(ResourceType resourceType) {
        boolean b = resourceTypeService.updateById(resourceType);
        if (b) {
            return R.ok().msg("修改成功");
        } else {
            return R.error().msg("修改失败");
        }
    }

    /**
     * 删除资源分类
     * @param id 资源分类ID
     * @return true
     */
    @GetMapping("/deleteResourceType/{id}")
    public R deleteResourceType(@PathVariable Long id) {
        boolean b = resourceTypeService.removeById(id);
        if (b) {
            return R.ok().msg("删除成功");
        } else {
            return R.error().msg("删除失败");
        }
    }

    /**
     * 批量删除
     * @param ids ids
     * @return true
     */
    @GetMapping("/deleteResourceTypes/{ids}")
    public R deleteResourceType(@PathVariable String ids) {
        List<Long> idList = StringSplitUtil.split(ids, ",");
        boolean b = resourceTypeService.removeByIds(idList);
        if (b) {
            return R.ok().msg("删除成功");
        } else {
            return R.error().msg("删除失败");
        }
    }

}

